package com.huang.builder;

public class BuilderPattern2 {

    public static void main(String[] args) {

        //第二种方式更加灵活，让客户端决定创建顺序
        //第一种方式有Director来决定创建顺序
        Phone phone = new Phone.Builder()
                .cpu("intel")
                .screen("三星屏幕")
                .memory("金士顿内存")
                .mainBoard("黑主板")
                .build();
        System.out.println(phone);

    }

}

class Phone{
    private String cpu;
    private String screen;
    private String memory;
    private String mainBoard;

    @Override
    public String toString() {
        return "Phone{" +
                "cpu='" + cpu + '\'' +
                ", screen='" + screen + '\'' +
                ", memory='" + memory + '\'' +
                ", mainBoard='" + mainBoard + '\'' +
                '}';
    }

    //如果使用建造者模式，私有构造方法，将Builder对象传给Phone
    private Phone(Builder builder){
        this.cpu = builder.cpu;
        this.screen = builder.screen;
        this.memory = builder.memory;
        this.mainBoard = builder.mainBoard;
    }

    static class Builder{
        private String cpu;
        private String screen;
        private String memory;
        private String mainBoard;

        public Builder cpu(String cpu){
            this.cpu= cpu;
            return this;
        }
        public Builder screen(String screen){
            this.screen= screen;
            return this;
        }
        public Builder memory(String memory){
            this.memory= memory;
            return this;
        }
        public Builder mainBoard(String mainBoard){
            this.mainBoard= mainBoard;
            return this;
        }

        //在builder中创建phone，
        public Phone build(){
            return new Phone(this);
        }
    }


}
